<?php
class BoatDAL {
	
	private $m_dbConnection;
	
	public function __construct ($a_dbConnection){
		$this->m_dbConnection = $a_dbConnection;	
	}
	
	
	public function GetBoatId ($memberId){
	
		$sql = "SELECT `BoatId`  FROM `Boat` WHERE `MemberId` = ? ";
		$stmt = $this->m_dbConnection->Prepare($sql);
		$stmt -> bind_param("i", $memberId);
		$stmt->execute();
		$stmt->bind_result($boatId);
		
		$Boats = array(); 
	
		while ($stmt->fetch()) {
			array_push($Boats, $boatId);	
		}
	
		if (count($Boats) >0){
			return $Boats;
		}else {
			return false;
		}
	}
	
	public function CountBoats($a_memberId){
		$sql = "
			SELECT COUNT( boat.MemberId ) 
			FROM boat
			NATURAL JOIN member
			WHERE boat.MemberId =?";
		$stmt = $this->m_dbConnection->Prepare($sql);
		$stmt -> bind_param("i", $a_memberId);
		
		$stmt->execute();
		$stmt -> bind_result($numberOfBoats);
		$stmt -> fetch ();
		return $numberOfBoats;
	}
	
	public function GetMemberBoats (){
		$sql = "
			SELECT BoatId, member.MemberId, `BoatInfo`,`BoatType`, `Length`  
			FROM  `boat` , `member`, `boattype` 
			WHERE boat.MemberId = member.MemberId AND boat.BoatTypeId= boattype.BoatTypeId
			";
		$stmt = $this->m_dbConnection->Prepare($sql);
		$stmt->execute();
		$stmt->bind_result($boatId, $memberId, $boatInfo, $boatType, $length);
		
	
		
		$i = 0;
		$boat = new Boat;
		$boatArray = array();
		
		while($stmt->fetch()){
			$boatArray [$i] = $boat->Create($boatId, $memberId, $boatType, $boatInfo, $length); 
			$i++;
		}
		return $boatArray;
	}
	
	
	public function AddBoatToMember($a_boatInfo, $a_boatTypeId, $a_memberId, $a_boatLength){
		$sql = "INSERT INTO `boat`(`BoatInfo`, `BoatTypeId`, `MemberId`,  `Length`) 
				VALUES (?,?,?, ?)";
		$stmt = $this->m_dbConnection->Prepare($sql);
		$stmt -> bind_param("siii", $a_boatInfo, $a_boatTypeId, $a_memberId, $a_boatLength);
		if ($stmt->execute()){ 
			return true;
		}else {
			return false;
		}
	}
	
	public function GetBoatTypes(){
		$sql= "SELECT `BoatTypeId`, `BoatType` FROM `boattype`";
		$stmt = $this->m_dbConnection->Prepare($sql);
		$stmt -> bind_result($boatTypeId, $boatType);

		if ($stmt->execute()){
			$boatTypeArray = array();
			$i = 0;
			while ($stmt -> fetch()){
				$boatTypeArray[$i] = array('boatTypeId'=>$boatTypeId, 'boatType'=>$boatType);
				$i++;
			}	
			return $boatTypeArray;
		}else {
			return false;
		}
	}

	public function DeleteBoat ($a_memberId){
		$sql = "DELETE boat FROM boat, member WHERE boat.MemberId = ?";
		$stmt = $this->m_dbConnection->Prepare($sql);
		$stmt -> bind_param("i", $a_memberId);
		if ($stmt->execute()){ 
			return true;
		}else {
			return false;
		}
	}
	public function UpdateBoat($a_getBoatInfo, $a_getBoatType, $a_getMemberId, $a_getBoatId, $a_getBoatLength){
		$sql ="UPDATE `boat` SET `BoatInfo`=?,`BoatTypeId`=?, `Length`=?  WHERE `MemberId`=? AND `BoatId` = ? ";
		$stmt = $this->m_dbConnection->Prepare($sql);
        $stmt -> bind_param("siii", $a_getBoatInfo, $a_getBoatType, $a_getBoatLength, $a_getMemberId);
		
		if ($stmt->execute()){
			return true;
		}else {
			return false;
		}	
	}
}








